﻿using Servo_Tool.constant;
using System;
using System.Text;

namespace Servo_Tool.utils
{
    public class MyHexUtils
    {
        /// <summary>
        /// 根据ushort[]数组，截取指定长度并转换为字符串
        /// </summary>
        /// <param name="src"></param>
        /// <param name="start"></param>
        /// <param name="len"></param>
        /// <param name="typ"></param>
        /// <returns></returns>
        public static string GetString(ushort[] src, int start, int len, string typ)
        {
            string value = "";
            ushort[] temp = new ushort[len];
            for (int i = 0; i < len; i++)
            {
                temp[i] = src[i + start];
            }


            if (SymbolConstants.DISPLAY_TYPE_A.Equals(typ))
            {
                //将ushort数组转换为byte数组
                byte[] bytesTemp = Ushorts2Bytes(temp, true);
                value = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' });
            }
            else if (SymbolConstants.DISPLAY_TYPE_D.Equals(typ))
            {
                //int类型
                byte[] bytesTemp = Ushorts2Bytes(temp);
                if (bytesTemp.Length == 2)
                {
                    value = BitConverter.ToUInt16(bytesTemp, 0).ToString();
                }
                if (bytesTemp.Length == 4)
                {
                    value = BitConverter.ToInt32(bytesTemp, 0).ToString();
                }
                if (bytesTemp.Length == 8)
                {
                    value = BitConverter.ToInt64(bytesTemp, 0).ToString();
                }
            }
            return value;
        }

        public static ushort[] SetString(string value, int len, string typ)
        {
            ushort[] temp = new ushort[len];
            if (len == 1)
            {
                temp[0] = Convert.ToUInt16(value);
            }
            else if (len == 2)
            {
                int i = Convert.ToInt32(value);
                temp[0] = (ushort)(i >> 16); // 取高16位
                temp[1] = (ushort)(i & 0x0000FFFF); // 取低16位
            }

            return temp;
        }

        /// <summary>
        /// 获取short类型数据
        /// </summary>
        /// <param name="src"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static short GetShort(ushort[] src, int start)
        {
            ushort[] temp = new ushort[1];
            temp[0] = src[start];
            byte[] bytesTemp = Ushorts2Bytes(temp);
            short res = BitConverter.ToInt16(bytesTemp, 0);
            return res;
        }

        /// <summary>
        /// 将ushort[]转换为byte[]
        /// </summary>
        /// <param name="src"></param>
        /// <param name="reverse"></param>
        /// <returns></returns>
        private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
        {

            int count = src.Length;
            byte[] dest = new byte[count << 1];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 8);
                    dest[i * 2 + 1] = (byte)(src[i] >> 0);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 0);
                    dest[i * 2 + 1] = (byte)(src[i] >> 8);
                }
            }
            return dest;
        }

        /// <summary>
        /// 将byte[]转换为ushort[]
        /// </summary>
        /// <param name="src"></param>
        /// <param name="reverse"></param>
        /// <returns></returns>
        private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
        {
            int len = src.Length;

            byte[] srcPlus = new byte[len + 1];
            src.CopyTo(srcPlus, 0);
            int count = len >> 1;

            if (len % 2 != 0)
            {
                count += 1;
            }

            ushort[] dest = new ushort[count];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
                }
            }

            return dest;
        }
    }
}
